package com.cskaoyan.controller.admin;

import com.cskaoyan.anno.LogAnnotation;
import com.cskaoyan.bean.pojo.MallAdmin;
import com.cskaoyan.bean.vo.AuthInfoVO;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.AuthService;
import com.cskaoyan.shiro.MallToken;
import com.cskaoyan.util.Md5Util;
import lombok.SneakyThrows;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
/**
 * @Author: zoey-zhu
 * @description:
 * @Date 21:35 2021/7/8
 * url:
 */
@RestController
@RequestMapping("admin/auth")
public class AuthController {

    @Autowired
    AuthService authService;

    /**
     * @Author: zoey-zhu
     * @description: login（POST）: 登录
     * @Date 21:35 2021/7/8
     * url: http://182.92.235.201:8083/admin/auth/login
     */
    @SneakyThrows
    @LogAnnotation
    @PostMapping("/login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        //具体的登录业务
        //看完shiro的部分，再把这部分补充完成 shiro权限管理框架
        //当前这个接口的data 👉 session

        String passwordMd5 = Md5Util.getMd5(password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(new MallToken(username, passwordMd5, "admin"));
        MallAdmin admin = (MallAdmin) subject.getPrincipal();

        if (admin != null) {
            return BaseRespVo.ok((String)(subject.getSession().getId()));
        } else { //登录失败
            BaseRespVo<String> baseRespVo = new BaseRespVo<>(null, "用户帐号或密码不正确", 605);
            return baseRespVo;
        }
    }


    /**
     * @Author: zoey-zhu
     * @description: info（GRT）: 登录后返回的信息
     * @Date 21:35 2021/7/8
     * url: http://182.92.235.201:8083/admin/auth/info?token=39dfa3d4-fed5-475f-b527-4361fb2a6d14
     */
    @GetMapping("/info")
    public BaseRespVo info(String token) {
        Subject subject = SecurityUtils.getSubject();
        MallAdmin admin = (MallAdmin) subject.getPrincipals().getPrimaryPrincipal();
        AuthInfoVO info = authService.info(admin);
        return BaseRespVo.ok(info);
    }

    /**
     * @Author harvey
     * @description: 处理一些没有登录或者没有权限的访问行为
     * @Date 2021/7/13 10:53
     */
    @RequestMapping("/unauth")
    public BaseRespVo unaunth() {
        return BaseRespVo.fail("没有登录或者没有权限!");
    }


    /**
     * @Author: zoey-zhu
     * @description: logout（POST）: 登录
     * @Date 22:45 2021/7/8
     * url: http://182.92.235.201:8083/admin/auth/logout
     */
    @LogAnnotation
    @PostMapping("/logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.getPrincipal() == null) {
            return BaseRespVo.fail("系统未登录 请重新登录");
        }
        subject.logout();
        return BaseRespVo.ok();
    }
}
